package org.taobao.ibatiside.ui.wizard;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.databinding.EMFDataBindingContext;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.widgets.Composite;

public abstract class PageComposite extends Composite implements
		PropertyChangeListener , Adapter{
	protected BatisSettingModel model;
	protected Notifier targetNotifier;
	protected BatisCommonPage hostPage;
	protected EMFDataBindingContext bindingContext;
	public void setErrorMessage(String newMessage) {
		hostPage.setErrorMessage(newMessage);
	}
	public void setMessage(String newMessage) {
		hostPage.setMessage(newMessage);
	}
	//for design time, don't call it
	public PageComposite(Composite parent, int style) {
		super(parent, style);
		createContent();
	}
	public PageComposite(Composite parent, int style, BatisCommonPage page) {
		super(parent, style);
		this.hostPage = page;
		this.model = page.model;
		createContent();
		doBinding(this.hostPage.getBindingContext());
		this.model.addPropertyChangeListener(this);
	}
	protected void doBinding(EMFDataBindingContext bindingContext) {
		
	}
	protected abstract void createContent();
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	@Override
	public void dispose() {
		if (this.model != null) {
			this.model.removePropertyChangeListener(this);
		}
		super.dispose();
	}
	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
	}
	@Override
	public void notifyChanged(Notification notification) {
		if (this.isEnabled()){
			doNotifyChanged(notification);
		}
	}
	protected abstract void doNotifyChanged(Notification notification);
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (this.isEnabled()){
			doPropertyChange(evt);
		}
	}

	protected abstract void doPropertyChange(PropertyChangeEvent evt) ;

	@Override
	public Notifier getTarget() {
		return targetNotifier;
	}

	@Override
	public void setTarget(Notifier newTarget) {
		targetNotifier = newTarget;
	}

	@Override
	public boolean isAdapterForType(Object type) {
		return this.isEnabled() && (type instanceof PageComposite);
	}
}
